﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
using System.Collections;

namespace BLL
{
    public class KyBLL
    {
        static int id = 0;
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        public IQueryable SelectDes()
        {
            return DB.Kies.OrderByDescending(s => s.MaKy).AsQueryable();
        }
        public string Insert(KyDTO ky)
        {
            try
            {
                Ky k = new Ky();
                k.MaKy = (id++).ToString();
                k.TenKy = ky.Tenky;
                k.HeSo = ky.Heso;
                DB.Kies.InsertOnSubmit(k);
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Update(KyDTO ky)
        {
            try
            {
                Ky k = DB.Kies.Where(s => s.MaKy == ky.Maky).FirstOrDefault();
                k.TenKy = ky.Tenky;
                k.HeSo = ky.Heso;
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Delete()
        {
            try
            {
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public List<KyDTO> getKy()
        {
            //lay het du lieu tu bang khoi
            List<Ky> list = DB.Kies.ToList<Ky>();
            //tao moi 1 doi tuong KhoiDTO de chua du lieu cua bang khoi
            List<KyDTO> listdto = new List<KyDTO>();
            foreach (Ky khoi in list)
            {
                KyDTO k = new KyDTO();
                k.Maky = khoi.MaKy;
                k.Tenky = khoi.TenKy;
                listdto.Add(k);
            }
            return listdto;
        }


    }
}
